package org.yinweichen.llm_dataset_backend.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.yinweichen.llm_dataset_backend.DTO.OriginalQuestionCreateDTO;
import org.yinweichen.llm_dataset_backend.DTO.QuestionSearchRequest;
import org.yinweichen.llm_dataset_backend.DTO.StandardQuestionCreateDTO;
import org.yinweichen.llm_dataset_backend.DTO.StandardQuestionUpdateDTO;
import org.yinweichen.llm_dataset_backend.entity.OriginalQuestion;
import org.yinweichen.llm_dataset_backend.entity.StandardQuestion;
import org.yinweichen.llm_dataset_backend.service.OriginalQuestionService;
import org.yinweichen.llm_dataset_backend.service.StandardQuestionService;

import java.util.List;

@RestController
@RequestMapping("/api/questions")
@RequiredArgsConstructor
public class QuestionController {
    private final OriginalQuestionService originalQuestionService;
    private final StandardQuestionService standardQuestionService;

    /**
     * @param request
     * 包含原始问题的版本号，以及问题内容等信息，id则自动生成
     */
    @PostMapping("/original/{version}")
    public void createOriginalQuestion(@PathVariable("version")String version,@RequestBody OriginalQuestionCreateDTO request) {
        originalQuestionService.createQuestion(version,request);
    }

    //不涉及更新原始问题的需求，要么新建，要么导入

//    /**
//     * 软删除原始问题，根据id和版本号进行精确匹配
//     */
//    @PutMapping("/original/inactive/{id}/{version}")
//    public void inactivateOriginalQuestion(@PathVariable Long id, @PathVariable String version) {
//        originalQuestionService.inactivateQuestion(id, version);
//    }
//
//    @PutMapping("/original/activate/{id}/{version}")
//    public void activateOriginalQuestion(@PathVariable Long id, @PathVariable String version) {
//        originalQuestionService.activateQuestion(id, version);
//    }

    @GetMapping("/original")
    public List<OriginalQuestion> searchOriginalQuestions(
            @RequestParam(required = false) String title,
            @RequestParam String version,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        if (title != null&&!title.isEmpty()) {
            return originalQuestionService.searchForQuestions(title, version);
        }
        return originalQuestionService.findAllQuestions(version, page, size);
    }

    //标准问题管理接口

    /**
     * 创建标准问题
     * @param question 包含标准问题详细信息的对象
     * @return ResponseEntity包装的创建结果
     */
    @PostMapping("/standard")
    public ResponseEntity<StandardQuestion> createStandardQuestion(@RequestBody StandardQuestionCreateDTO question) {
        return standardQuestionService.create(question);
    }

    /**
     * 更新标准问题
     * @param question 包含更新后信息的标准问题对象
     */
    @PutMapping("/standard")//TODO: 接口改动
    public void updateStandardQuestion(@RequestBody StandardQuestionUpdateDTO question) {
        standardQuestionService.update(question);
    }

//    /**
//     * 停用标准问题(软删除)
//     * 通过设置active=false实现，数据仍保留在数据库中
//     * @param id 要停用的标准问题ID
//     * @return ResponseEntity包装的操作结果
//     */
//    @PutMapping("/standard/inactivate/{id}")
//    public ResponseEntity<Void> inactivateStandardQuestion(@PathVariable Long id) {
//        return standardQuestionService.inactivate(id);
//    }
//
//    /**
//     * 切换活跃状态
//     * @param id 要停用的标准问题ID
//     * @return ResponseEntity包装的操作结果
//     */
//    @PutMapping("/standard/activate/{id}")//TODO: 版本号缺失
//    public ResponseEntity<Void> activateStandardQuestion(@PathVariable Long id) {
//        return standardQuestionService.activate(id);
//    }

    @PutMapping("/standard/switch/{id}/{version}")//TODO: 版本号缺失1
    public ResponseEntity<Void> switchStandardQuestionActiveStatus(@PathVariable Long id,@PathVariable String version) {
        return standardQuestionService.switchActiveStatus(id,version);
    }

    /**
     * 根据ID获取标准问题
     * @param id 标准问题ID
     * @return ResponseEntity包装的查询结果
     */
    @GetMapping("/standard/{id}/{version}")//TODO: 版本号缺失1
    public ResponseEntity<StandardQuestion> getStandardQuestionById(@PathVariable Long id, @PathVariable String version) {
        return standardQuestionService.getById(id,version);
    }

    /**
     * 获取所有活跃的标准问题(分页)
     * @param version 版本号
     * @param page 页码(从0开始)
     * @param size 每页大小
     * @return ResponseEntity包装的分页查询结果
     */
    @GetMapping("/standard/active/{version}")
    public ResponseEntity<List<StandardQuestion>> findAllActiveStandardQuestions(
            @PathVariable String version,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return standardQuestionService.findAllActive(version, page, size);
    }

    @GetMapping("/standard/no-answer/{version}")
    public ResponseEntity<List<StandardQuestion>> findQuestionsWithoutAnswers(
            @PathVariable String version,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return standardQuestionService.findQuestionsWithoutAnswers(version, page, size);
    }

    @GetMapping("/standard/inactive/{version}")
    public ResponseEntity<List<StandardQuestion>> findInactiveQuestions(
            @PathVariable String version,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return standardQuestionService.findInactiveQuestions(version, page, size);
    }

    /**
     * 根据原始问题ID获取关联的标准问题
     * @param originalId 原始问题ID
     * @param version 版本号
     * @return ResponseEntity包装的查询结果
     */
    @GetMapping("/standard/by-original/{originalId}/{version}")
    public ResponseEntity<List<StandardQuestion>> getStandardQuestionsByOriginalQuestion(
            @PathVariable Long originalId,
            @PathVariable String version) {
        return standardQuestionService.findByOriginalQuestion(originalId, version);
    }

    /**
     * 统计搜索得到的问题个数
     * @param title 搜索关键词
     * @param version 版本号
     * @return 匹配的问题数量
     */
    @GetMapping("/original/search-count")
    public Long countSearchResults(@RequestParam(required = false) String title,
                                 @RequestParam String version) {
        return originalQuestionService.countSearchResults(title, version);
    }

    @GetMapping("/standard/no-answer/{version}/count")
    public Long countStandardQuestionsNoAnswer(@PathVariable String version) {
        return standardQuestionService.countQuestionsWithoutAnswers(version);
    }

    @GetMapping("/standard/active/{version}/count")
    public Long countStandardQuestionsActive(@PathVariable String version) {
        return standardQuestionService.countActiveQuestions(version);
    }

    @GetMapping("/standard/inactive/{version}/count")
    public Long countStandardQuestionsInactive(@PathVariable String version) {
        return standardQuestionService.countInactiveQuestions(version);
    }
    @PostMapping("/standard/search")
    public ResponseEntity<Page<StandardQuestion>> searchQuestions(
            @RequestBody QuestionSearchRequest searchRequest,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        return standardQuestionService.searchQuestions(searchRequest, page, size);
    }
}
